package network.editor;

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import javax.swing.TransferHandler;

import network.action.EditorActions;
import xujun.control.XContorlUtil;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.util.mxGraphActions;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxGraphView;

public class EditorToolBar extends JToolBar {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private boolean ignoreZoomChange = false;
	private BasicNetworkEditor networkEditor;

	public EditorToolBar(final BasicNetworkEditor editor) {
		setFloatable(false);
		setRollover(true);// 鼠标悬停效果
		networkEditor = editor;
		add(bind("新建", "images/new.png", new EditorActions.NewAction()));
		add(bind("打开", "images/open.png", new EditorActions.OpenAction()));
		add(bind("保存", "images/save.png", new EditorActions.SaveAction(false)));
		add(bind("打印", "images/print.png", new EditorActions.PrintAction()));
		addSeparator();
		add(bind("剪切", "images/cut.png", TransferHandler.getCutAction()));
		add(bind("复制", "images/copy.png", TransferHandler.getCopyAction()));
		add(bind("粘贴", "images/paste.png", TransferHandler.getPasteAction()));
		add(bind("删除", "images/delete.png", mxGraphActions.getDeleteAction()));
		addSeparator();
		add(bind("撤销", "images/undo.png", new EditorActions.HistoryAction(true)));
		add(bind("恢复", "images/redo.png",
				new EditorActions.HistoryAction(false)));
		addSeparator();
		// 加载字体
		GraphicsEnvironment env = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		List<String> fonts = new ArrayList<String>();
		fonts.addAll(Arrays.asList(new String[] { "Courier New",
				"Times New Roman", }));
		fonts.addAll(Arrays.asList(env.getAvailableFontFamilyNames()));
		final JComboBox fontCombo = new JComboBox(fonts.toArray());
		fontCombo.setEditable(true);
		fontCombo.setMinimumSize(new Dimension(120, 0));
		fontCombo.setPreferredSize(new Dimension(120, 0));
		fontCombo.setMaximumSize(new Dimension(120, 100));
		add(fontCombo);

		fontCombo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String font = fontCombo.getSelectedItem().toString();
				if (font != null && !font.equals("-")) {
					mxGraph graph = networkEditor.getGraphComponent()
							.getGraph();
					graph.setCellStyles(mxConstants.STYLE_FONTFAMILY, font);
				}
			}
		});

		final JComboBox sizeCombo = new JComboBox(new Object[] { "6pt", "8pt",
				"9pt", "10pt", "12pt", "14pt", "18pt", "24pt", "30pt", "36pt",
				"48pt", "60pt" });
		sizeCombo.setEditable(true);
		sizeCombo.setMinimumSize(new Dimension(65, 0));
		sizeCombo.setPreferredSize(new Dimension(65, 0));
		sizeCombo.setMaximumSize(new Dimension(65, 100));
		add(sizeCombo);
		sizeCombo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mxGraph graph = networkEditor.getGraphComponent().getGraph();
				graph.setCellStyles(mxConstants.STYLE_FONTSIZE, sizeCombo
						.getSelectedItem().toString().replace("pt", ""));
			}
		});
		addSeparator();
		add(bind("粗体", "images/bold.png", new EditorActions.FontStyleAction(
				true)));
		add(bind("斜体", "images/italic.png", new EditorActions.FontStyleAction(
				false)));
		add(bind("文字颜色", "images/fontcolor.png", new EditorActions.ColorAction(
				"Font", mxConstants.STYLE_FONTCOLOR)));
		add(bind("线条颜色", "images/stroke.png", new EditorActions.ColorAction(
				"Stroke", mxConstants.STYLE_STROKECOLOR)));
		add(bind("填充颜色", "images/fillcolor.png", new EditorActions.ColorAction(
				"Stroke", mxConstants.STYLE_FILLCOLOR)));
		addSeparator();
		final mxGraphView view = networkEditor.getGraphComponent().getGraph()
				.getView();
		final JComboBox zoomCombo = new JComboBox(new Object[] { "400%",
				"200%", "150%", "100%", "75%", "50%", "页面" });
		zoomCombo.setEditable(true);
		zoomCombo.setMinimumSize(new Dimension(75, 0));
		zoomCombo.setPreferredSize(new Dimension(75, 0));
		zoomCombo.setMaximumSize(new Dimension(75, 100));
		zoomCombo.setMaximumRowCount(9);
		add(zoomCombo);
		mxIEventListener scaleTracker = new mxIEventListener() {
			public void invoke(Object sender, mxEventObject evt) {
				ignoreZoomChange = true;
				try {
					zoomCombo.setSelectedItem((int) Math.round(100 * view
							.getScale()) + "%");
				} finally {
					ignoreZoomChange = false;
				}
			}
		};
		view.getGraph().getView().addListener(mxEvent.SCALE, scaleTracker);
		view.getGraph().getView()
				.addListener(mxEvent.SCALE_AND_TRANSLATE, scaleTracker);
		scaleTracker.invoke(null, null);
		zoomCombo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mxGraphComponent graphComponent = networkEditor
						.getGraphComponent();
				if (!ignoreZoomChange) {
					String zoom = zoomCombo.getSelectedItem().toString();
					if (zoom.equals("页面")) {
						graphComponent.setPageVisible(true);
						graphComponent
								.setZoomPolicy(mxGraphComponent.ZOOM_POLICY_PAGE);
					} else {
						try {
							zoom = zoom.replace("%", "");
							double scale = Math.min(16, Math.max(0.01,
									Double.parseDouble(zoom) / 100));
							graphComponent.zoomTo(scale,
									graphComponent.isCenterZoom());
						} catch (Exception ex) {
							JOptionPane.showMessageDialog(networkEditor,
									ex.getMessage());
						}
					}
				}
			}
		});
	}

	/**
	 * 创建一个和EditorAction绑定的按钮
	 * 
	 * @param tooltip
	 * @param iconUrl
	 * @param action
	 * @return
	 */
	public JButton bind(String tooltip, String iconUrl, final Action action) {
		JButton btn = new JButton(XContorlUtil.getImageIcon(iconUrl));
		btn.setToolTipText(tooltip);
		// 按钮事件触发绑定的EditorAction事件
		btn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				action.actionPerformed(new ActionEvent(networkEditor
						.getGraphComponent(), e.getID(), e.getActionCommand()));
			}
		});
		return btn;
	}
}